package com.FH.permission.member_role;


import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by acer on 2016/8/1.
 */
@Stateless
public class MemberRoleDAOImpl extends AccessDB implements MemberRoleDAO {
    @Override
    public ResultDTO getMemberRoleInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject jsonObject = new JSONObject(search);

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }


            if (jsonObject.has("member_id")) {//角色id
                sql_w += " AND member_id ='" + jsonObject.getString("member_id")  + "'";
            }

            String sqlexit = "select count(1) from  member_role  where 1=1 ";
            String sqlexit_ok = sqlexit + sql_w;

            String existName = getReportingToOne(sqlexit_ok);//查询此数据是否存在

            List<String[]> seresult = new ArrayList<String[]>();//存储查询到的结果

            List<MemberRoleShowDTO> result = new ArrayList<MemberRoleShowDTO>();

            if(!existName.equals("0")){//数据存在

                String sql="select m.member_role_id,m.member_id,m.memeber_role_create,m.member_role_create_time," +
                        "m.member_role_modify,m.member_role_modify_time,m.sys_role_id,s.sys_role_name,s.sys_role_modify from member_role m,sys_role s where m.sys_role_id=s.sys_role_id"+sql_w+sql_page;

                seresult = this.getReportingTo(sql);
                System.out.print(sql);
            }

            if(seresult.size()>0){

                for(int i=0;i<seresult.size();i++){

                    MemberRoleShowDTO memberRoleShowDTO=new MemberRoleShowDTO();
                    memberRoleShowDTO.setSys_role_id(seresult.get(i)[1]);
                    memberRoleShowDTO.setMember_id(seresult.get(i)[2]);
                    memberRoleShowDTO.setMemeber_role_create(seresult.get(i)[3]);
                    memberRoleShowDTO.setMember_role_create_time(seresult.get(i)[4]);
                    memberRoleShowDTO.setMember_role_modify(seresult.get(i)[5]);
                    memberRoleShowDTO.setMember_role_modify_time(seresult.get(i)[6]);
                    memberRoleShowDTO.setSys_role_id(seresult.get(i)[7]);
                    memberRoleShowDTO.setSys_role_name(seresult.get(i)[8]);
                    memberRoleShowDTO.setSys_role_modify(seresult.get(i)[9]);

                    result.add(memberRoleShowDTO);
                }
                resultDTO.setValues(result);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            }else{
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

            return resultDTO;
    }

    /**
     * 插入数据
     * @param memberRolePullDTO
     * @return
     */
    @Override
    public ResultDTO addMemberRoleInfo(MemberRolePullDTO memberRolePullDTO) {

        int resultls = 0;


        //构建返回对象
        ResultDTO resultDTO = new ResultDTO();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式化

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        if(memberRolePullDTO.getMember_id()!=null && memberRolePullDTO.getSys_role_id()!=null){
              String member_id=memberRolePullDTO.getMember_id();
              String sys_role_id=memberRolePullDTO.getSys_role_id();
            String sql="select * from member_role where member_id='"+member_id+"' and sys_role_id='"+sys_role_id+"'";
            String sum= getReportingToOne(sql);
            if(!sum.equals("")){
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion("角色已存在请重新分配角色");
                return resultDTO;
            }
        }

        String MemberRole_insert="";
               MemberRole_insert="insert into member_role(member_role_id,member_id,memeber_role_create,member_role_create_time,member_role_modify,member_role_modify_time,sys_role_id)" +
                       "values('"+uuid+"','"+memberRolePullDTO.getMember_id()+"','"+memberRolePullDTO.getMemeber_role_create()+"','"+df.format(new Date())+"','"+memberRolePullDTO.getMember_role_modify()+"'," +
                       "'"+df.format(new Date())+"','"+memberRolePullDTO.getSys_role_id()+"')";

        //插入业务表
        resultls =  addReportingTo(MemberRole_insert);

        if(resultls!=0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }

        return resultDTO;
    }
}
